const {initConnection} = require('../util/initMq')

async function producer() {
    // 交换机名称
    const testExchange = 'dream';
    // 队列名称
    const testQueue = 'dream_queue';
    // 死信队列名称
    const testExchangeDLX = 'dram_dxl';
    // 死信队列路由 一个队列queue可以按照不同的route进行分发
    const testRoutingKeyDLX = 'dream_route';

    const connection = await initConnection();
    // 创建一个频道
    const ch = await connection.createChannel();
    // 绑定一个交换机 类型为direct
    await ch.assertExchange(testExchange, 'direct', {durable: true});
    // 设定队列
    const queueResult = await ch.assertQueue(testQueue, {
        exclusive: false,
        deadLetterExchange: testExchangeDLX,
        deadLetterRoutingKey: testRoutingKeyDLX,
    });
    await ch.bindQueue(queueResult.queue, testExchange);
    const msg = 'hello world three!';
    console.log('producer msg：', msg);
    await ch.sendToQueue(queueResult.queue, Buffer.from(msg), {
        expiration: 3000
    });
    ch.close();
}


producer();
